import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

/// 动画序列
class TweensequenceAnimation extends StatefulWidget {
  TweensequenceAnimation({this.text}) : super();

  final String? text;

  @override
  State<StatefulWidget> createState() {
    return TweensequenceState();
  }
}

class TweensequenceState extends State<TweensequenceAnimation>
    with TickerProviderStateMixin {
  AnimationController? _controller;
  AnimationController? _animationController;
  Animation? _colorAnimation;
  Animation? _sizeAnimation;
  Animation<double>? _animation;

  @override
  void initState() {
    super.initState();

    _animationController =
        AnimationController(duration: Duration(milliseconds: 500), vsync: this)
          ..addListener(() {
            setState(() {});
          });
    //颜色控制
    _colorAnimation = ColorTween(begin: Colors.red, end: Colors.blue).animate(
        CurvedAnimation(
            parent: _animationController!, curve: Interval(0.0, 0.8)));
    //大小控制
    ///Interval中begin 和end参数值的范围是0.0到1.0。 权重动画从0.5（一半）开始到结束，如果动画时长为6秒，_sizeAnimation则从第3秒开始。
    _sizeAnimation = Tween(begin: 100.0, end: 300.0).animate(CurvedAnimation(
        parent: _animationController!, curve: Interval(0.2, 1.0)));

    _animationController?.addListener(() {
      setState(() {});
    });
    _animationController?.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        _animationController?.reverse();
      } else if (status == AnimationStatus.dismissed) {
        //动画恢复到初始状态时执行动画（正向）
        _animationController?.forward();
      }
    });
    //开始动画
    _animationController?.forward();

    /////////////////////////////////////////////////////////////////////////////////////
    _controller =
        AnimationController(duration: Duration(milliseconds: 500), vsync: this)
          ..addListener(() {
            setState(() {});
          });

    _animation = TweenSequence([
      TweenSequenceItem(
          tween: Tween(begin: 50.0, end: 100.0)
              .chain(CurveTween(curve: Curves.easeIn)),
          weight: 40),
      TweenSequenceItem(tween: ConstantTween<double>(100.0), weight: 20),
      TweenSequenceItem(tween: Tween(begin: 100.0, end: 200.0), weight: 40),
    ]).animate(_controller!);
    // _controller.addListener(() {
    //   setState(() {
    //   });
    // });
    _controller?.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        _controller?.reverse();
      } else if (status == AnimationStatus.dismissed) {
        //动画恢复到初始状态时执行动画（正向）
        _controller?.forward();
      }
    });
    //开始动画
    _controller?.forward();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.text ?? ""),
        ),
        body: Center(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              Container(
                  height: _sizeAnimation?.value,
                  width: _sizeAnimation?.value,
                  color: _colorAnimation?.value),
              SizedBox(
                height: 60,
              ),
              Container(
                  height: _animation?.value,
                  width: _animation?.value,
                  color: Colors.deepOrange),
            ],
          ),
        ));
  }

  @override
  void dispose() {
    _animationController?.dispose();
    _controller?.dispose();
    super.dispose();
  }
}
